<?php

namespace app\admin\controller;

use app\admin\model\Comment as ModelComment;
use app\BaseController;
use think\facade\Session;

class Comment extends Base
{
    public function index()
    {
        $this->logs('[管理员]评论视图');
        return view();
    }
    public function list()
    {
        $this->logs('[管理员]评论列表');
        $list = ModelComment::with(['userProfile' => function ($query) {
            $query->field('id , username');
        }])->order('id desc')->paginate(10);
        foreach ($list as $k => $v) {
            $list[$k]['username'] = $v['userProfile']['username'];
        }
        $data = [
            "code" => 0,
            'msg' => '',
            'count' => $list->total(),
            'data' => $list
        ];
        return json($data);
    }
    public function del($id)
    {
        $this->logs('[管理员]删除评论');
        $isHas = ModelComment::findOrEmpty($id)->isEmpty();
        if ($isHas) return error("不存在评论[$id]");
        else {
            $res = ModelComment::destroy($id);
            if ($res) return success();
            else return error();
        }
    }
}
